from pms.common.views import PostFormula
from user.utils.update import update_user_by_view
from user.utils.profile import user_view
from user.models import User


class API(PostFormula):

    def handle(self, req):
        uid = req['id']
        view = req['mod']
        user = User.objects.get(id=uid)
        if user == self.user:  # 当前用户
            self.assert_one_of_permissions([5, 6])
            updated_user = update_user_by_view(uid, view, self=True)
        else:  # 其他用户
            self.assert_permissions([6])
            updated_user = update_user_by_view(uid, view, self=False)
        return self.success_respond(user_view(updated_user))
